package com.agame.spidercard.sprite;

import org.andengine.entity.Entity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.agame.spidercard.CardList;
import com.agame.spidercard.SpiderGame;
import com.agame.spidercard.adt.card.Card;

public class CardSprite extends Sprite {
	private Card card;
	private CardList parent;
	private SpiderGame game;
	private float ax, y;

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	public CardList getParent() {
		return parent;
	}

	public void setParent(CardList parent) {
		this.parent = parent;
	}

	public CardSprite(float pX, float pY, ITextureRegion pTextureRegion,
			VertexBufferObjectManager pSpriteVertexBufferObject, Card card,
			SpiderGame game) {
		super(pX, pY, pTextureRegion, pSpriteVertexBufferObject);
		this.card = card;
		this.game = game;
	}

	boolean mGrabbed = false;

	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
			final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if (!parent.canDrag(this))
			return false;

		switch (pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
//			game.getmScene().detachChild(this);
//			game.getmScene().attachChild(this);
			ax = getX();
			y = getY();
			Log.v("CardSprit", card.mColor + "  VS  " + card.mValue);
			this.setScale(1.25f);
			this.mGrabbed = true;
			break;
		case TouchEvent.ACTION_MOVE:
			if (this.mGrabbed) {
//				this.setPosition(pSceneTouchEvent.getX() - Card.CARD_WIDTH / 2,
//						pSceneTouchEvent.getY() - Card.CARD_HEIGHT / 2);
				parent.dragCards(pSceneTouchEvent, this) ;
			}
			break;
		case TouchEvent.ACTION_UP:
			if (this.mGrabbed) {
				this.mGrabbed = false;
				this.setScale(1.0f);
				 setPosition(getPostionX(pSceneTouchEvent.getX()), y) ;
				getPostionX(pSceneTouchEvent.getX());
				parent.getMoveSpriteList().clear() ;
				game.updatePosition();
			}
			break;
		}
		return true;
	}

	private float getPostionX(float x) {

		if (x > SpiderGame.cols[0] && x < SpiderGame.cols[0] + Card.CARD_WIDTH) {
			CardList list = SpiderGame.cardlistMap.get(SpiderGame.cols[0]);
			Card mCard = list.getCardSpriteList()
					.get(list.getCardSpriteList().size() - 1).getCard();
			if (card.mValue.ordinal() + 1 != mCard.mValue.ordinal()) {
				return ax;
			}
			parent.getCardSpriteList().remove(this);
			parent = SpiderGame.cardlistMap.get(SpiderGame.cols[0]);
			parent.getCardSpriteList().add(this);
			return SpiderGame.cols[0];
		}
		if (x > SpiderGame.cols[1] && x < SpiderGame.cols[1] + Card.CARD_WIDTH) {
			CardList list = SpiderGame.cardlistMap.get(SpiderGame.cols[1]);
			Card mCard = list.getCardSpriteList()
					.get(list.getCardSpriteList().size() - 1).getCard();
			if (card.mValue.ordinal() + 1 != mCard.mValue.ordinal()) {
				return ax;
			}
			parent.getCardSpriteList().remove(this);
			parent = SpiderGame.cardlistMap.get(SpiderGame.cols[1]);
			parent.getCardSpriteList().add(this);
			return SpiderGame.cols[1];
		}
		if (x > SpiderGame.cols[2] && x < SpiderGame.cols[2] + Card.CARD_WIDTH) {
			CardList list = SpiderGame.cardlistMap.get(SpiderGame.cols[2]);
			Card mCard = list.getCardSpriteList()
					.get(list.getCardSpriteList().size() - 1).getCard();
			if (card.mValue.ordinal() + 1 != mCard.mValue.ordinal()) {
				return ax;
			}
			parent.getCardSpriteList().remove(this);
			parent = SpiderGame.cardlistMap.get(SpiderGame.cols[2]);
			parent.getCardSpriteList().add(this);
			return SpiderGame.cols[2];
		}
		if (x > SpiderGame.cols[3] && x < SpiderGame.cols[3] + Card.CARD_WIDTH) {
			CardList list = SpiderGame.cardlistMap.get(SpiderGame.cols[3]);
			Card mCard = list.getCardSpriteList()
					.get(list.getCardSpriteList().size() - 1).getCard();
			if (card.mValue.ordinal() + 1 != mCard.mValue.ordinal()) {
				return ax;
			}
			parent.getCardSpriteList().remove(this);
			parent = SpiderGame.cardlistMap.get(SpiderGame.cols[3]);
			parent.getCardSpriteList().add(this);
			return SpiderGame.cols[3];
		}
		if (x > SpiderGame.cols[4] && x < SpiderGame.cols[4] + Card.CARD_WIDTH) {
			CardList list = SpiderGame.cardlistMap.get(SpiderGame.cols[4]);
			Card mCard = list.getCardSpriteList()
					.get(list.getCardSpriteList().size() - 1).getCard();
			if (card.mValue.ordinal() + 1 != mCard.mValue.ordinal()) {
				return ax;
			}
			parent.getCardSpriteList().remove(this);
			parent = SpiderGame.cardlistMap.get(SpiderGame.cols[4]);
			parent.getCardSpriteList().add(this);
			return SpiderGame.cols[4];
		}
		if (x > SpiderGame.cols[5] && x < SpiderGame.cols[5] + Card.CARD_WIDTH) {
			CardList list = SpiderGame.cardlistMap.get(SpiderGame.cols[5]);
			Card mCard = list.getCardSpriteList()
					.get(list.getCardSpriteList().size() - 1).getCard();
			if (card.mValue.ordinal() + 1 != mCard.mValue.ordinal()) {
				return ax;
			}
			parent.getCardSpriteList().remove(this);
			parent = SpiderGame.cardlistMap.get(SpiderGame.cols[5]);
			parent.getCardSpriteList().add(this);
			return SpiderGame.cols[5];
		}
		return ax;
	}
}
